package com.yxhl.zoume.core.coupon.presenter;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.coupon.view.HistoryCouponView;
import com.yxhl.zoume.data.http.rest.param.coupon.HistoryCouponParam;
import com.yxhl.zoume.data.http.rest.response.coupon.HistoryCouponResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.coupon.HistoryCouponUseCase;

import javax.inject.Inject;

/**
 * Created by vhawk on 2016/11/3.
 */

public class HistoryCouponPresenter extends BasePresenter {

    @Inject
    HistoryCouponUseCase useCase;
    private HistoryCouponView view;

    @Inject
    public HistoryCouponPresenter(Context context) {
        super(context);
    }

    @Override
    public void attachView(BaseView view) {
        super.attachView(view);
        this.view = (HistoryCouponView) view;
    }


    public void fetchHistoryCoupon() {
        HistoryCouponParam param = new HistoryCouponParam();
        param.setMobile(getMobile())
                .setToken(getToken());
        useCase.execute(param, new ZMSubscriber<HistoryCouponResponse>() {
            @Override
            public void onNext(HistoryCouponResponse response) {
                super.onNext(response);
                if (response.isSucc()) {
                    view.handleFetchHistoryCouponSucceed(response.getData().getCoupons());
                } else {
                    view.handleFetchHistoryCouponFailure(response.getResultMessage());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.handleFetchHistoryCouponError();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        useCase.unsubscribe();
    }
}
